package queries;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.TimeUnit;

import entitys.Customer;
import entitys.Employee;
import entitys.LoginUser;
import entitys.Request;
import enums.Position;
import enums.Result;
import enums.Status;


public class GetCustomersWithPermissionQuery {
/**
 * Execute query
 * @param r
 * @param conn
 * @return
 */
	public static ArrayList<Customer> exe(Request r,Connection conn) {
		ArrayList<Customer> arr = new ArrayList<Customer>();
		ResultSet res = null; 
		Statement stmt;
		Calendar date = Calendar.getInstance();
		long days;
		long diff;
			try {
				stmt = conn.createStatement();
				res = stmt.executeQuery("SELECT * from ccrm.customer,ccrm.marketingsalesaddressing AS m WHERE customer.permit='1' AND m.empID='"+r.getUserID()+"' AND m.addressed='0' AND customer.id=m.customerID AND m.marketingSaleID='"+r.getEntity()+"'; ");
				

				while(res.next())
				{
					
					diff=date.getTime().getTime()-res.getDate(9).getTime();
					 days = diff / (24 * 60 * 60 * 1000);
					
					 if(days>=7)
					 {
						 arr.add(new Customer(res.getString(2), res.getString(3), res.getString(6), res.getString(4), res.getString(5), res.getString(1), res.getInt(7)==1,res.getString(8)));
					 }
					//      new Customer(fname, lname, address, phone, email, id, permit, birth)
				}
			} catch (Exception e) {

				e.printStackTrace();
			}
			
		
		return arr;
		
	}
}
